/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DLL;

/**
 *
 * @author PyyPyLyn
 */

import java.io.IOException;
import DLL.*;
import java.net.*;

public class SendFunction {
    
    public static boolean SendPacket(DatagramSocket d_Socket, InetAddress Destination, byte[] Data, int DestPort)throws Exception
    {
        try
        {
            DatagramPacket packet = new DatagramPacket(Data, Data.length, Destination, DestPort);
            d_Socket.send(packet);
        }
        catch(Exception ex)
        {
            return false;
        }
        finally
        {            
            d_Socket.close();
        }
        return true;
    }
    public static boolean SendPacketData(DatagramSocket d_Socket, InetAddress Destination, byte[] Data, int DestPort)throws Exception
    {
        try
        {
            DatagramPacket packet = new DatagramPacket(Data, Data.length, Destination, DestPort);
            d_Socket.send(packet);
        }
        catch(Exception ex)
        {
            return false;
        }
        
        return true;
    }
    
    public static boolean SendDataPacket(DatagramSocket d_Socket, InetAddress Destination, byte[] Data)throws Exception
    {
        return SendPacket(d_Socket,Destination,Data,4447);
    }
    
    public static boolean SendRequestDownload(DatagramSocket d_Socket, InetAddress Destination, String chunkName, int port) throws Exception
    {
        
        String data = chunkName +","+ port;
        byte[] datasend = QueryPackets.SendNormal("3", data);
        return SendPacket(d_Socket, Destination, datasend,4448);
        
    }
    
    public static boolean SendDynamicPort(DatagramSocket d_Socket, InetAddress Destination, byte[] Data)throws Exception
    {
        try
        {
            DatagramPacket packet = new DatagramPacket(Data, Data.length, Destination, d_Socket.getPort());
            d_Socket.send(packet);
        }
        catch(Exception e)
        {
            return false;
        }       
        return true;
    }
    
   
    
    public static boolean SendBroadCastPacket(MulticastSocket m_Socket, InetAddress Group, byte[] Data) throws Exception
    {        
        try
        {            
            DatagramPacket packet = new DatagramPacket(Data, Data.length, Group, m_Socket.getLocalPort());
            m_Socket.send(packet);
        }
        catch (Exception e)
        {
            System.out.print(e.getMessage());
            return false;
        }        
        return true;
    }
    
    
        
}
